<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            width: 300px;
            height: 100px;
        }
        
        button {
            display: block;
            margin-left: 260px;
            margin-top: 10px;
        }
        
        ul {
            width: 300px;
            height: 400px;
            margin-top: 30px;
        }
        
        li a {
            float: right;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <input type="text" name="" id="">
    <button>发布</button>
    <ul>
        <!-- <li>123</li> -->

    </ul>
    <script>
        var but = document.querySelector('button');
        var ul = document.querySelector('ul');
        var inp = document.querySelector('input');


        // while (inp.innerText != '') {
        but.onclick = function() {
                // 创建节点
                var li = document.createElement('li');
                // var remove = createElement('button');
                if (inp.value == '') {
                    alert('没有输入内容');
                    return false;
                } else {
                    // var remove = createElement('a');
                    li.innerHTML = inp.value + '      <a href="javascript:;">删除</a>';
                    // button.innerHTML = '删除';
                    // 添加节点
                    // ul.appendChild(li);  
                    ul.insertBefore(li, ul.children[0])
                        // li.insertBefore(remove, li.children[0])

                    inp.value = '';
                    // 删除元素
                    var as = document.querySelectorAll('a')
                    for (i = 0; i < as.length; i++) {
                        as[i].onclick = function() {
                            //    node.removeChild(child)
                            ul.removeChild(this.parentNode);


                        }
                    }
                }

            }
            // }
    </script>
</body>

</html>